package com.wutuo.aggragation.report.dao;

import cn.apestar.core.database.BaseMapper;
import com.wutuo.aggragation.report.response.ListMonthTaskDataItem;
import com.wutuo.aggragation.report.response.ListMonthTaskDetailItem;
import com.wutuo.aggragation.report.vo.TaskTypeMonthSignNumber;
import com.wutuo.aggragation.rollcall.vo.SimpleTaskVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 标题、简要说明. <br> 类详细说明.
 * <p/>
 * Copyright: Copyright (c) 2016年04月30日 10:31
 * <p/>
 * Company:
 * <p/>
 *
 * @author Kent
 * @version 1.0.0
 */
public interface SignReportMapper extends BaseMapper {

    @Select({ "select distinct task_type as taskType,date_format(t.task_time,'%Y%m') as taskMonth from task t ",
        "where t.del_flag = 1 and t.institution_id = #{institutionId,jdbcType=BIGINT} order by taskMonth desc" })
    @ResultType(value = SimpleTaskVo.class)
    List<SimpleTaskVo> statisTaskTypeForMonth(@Param("institutionId") Long institutionId);

    @Select({ " select td.user_id as childId,t.task_type as taskType,count(1) as signNum ", "  from task_detail td ",
        " inner join task t on td.task_id = t.id and t.del_flag =1 ",
        " where date_format(td.create_date,'%Y%m') = #{month,jdbcType=VARCHAR} ",
        " and t.institution_id = #{institutionId,jdbcType=BIGINT} and td.del_flag = 1",
        " and t.task_type = #{taskType,jdbcType=VARCHAR}  group by user_id,t.task_type" })
    @ResultType(value = ListMonthTaskDataItem.class)
    List<ListMonthTaskDataItem> statsMonthAndType(@Param("month") String month, @Param("taskType") String taskType,
                                                  @Param("institutionId") Long institutionId);

    @Select({ "SELECT t.task_type AS taskType, DATE_FORMAT(t.task_time,'%Y%m') AS taskMonth, COUNT(1) AS signNum",
        "FROM task_detail td", "INNER JOIN task t ON td.task_id = t.id AND t.del_flag =1",
        "WHERE t.institution_id =  #{institutionId,jdbcType=BIGINT} and td.del_flag = 1",
        "GROUP BY taskType,taskMonth order by taskMonth desc" })
    @ResultType(value = TaskTypeMonthSignNumber.class)
    List<TaskTypeMonthSignNumber> statsTaskTypeMonthSignNumber(@Param("institutionId") Long institutionId);

    @Select({ " select distinct date_format(td.create_date,'%Y%m%d') as day ", "from task_detail td ",
        " inner join task t on td.task_id = t.id and t.del_flag =1 ",
        " where  date_format(td.create_date,'%Y%m') =  #{month,jdbcType=VARCHAR} and t.institution_id = #{institutionId,jdbcType=BIGINT}",
        " and td.user_id = #{childId,jdbcType=BIGINT}  and t.task_type = #{taskType,jdbcType=VARCHAR}",
        " and td.del_flag = 1" })
    List<ListMonthTaskDetailItem> statsSignDay(@Param("month") String month, @Param("taskType") String taskType,
                                               @Param("institutionId") Long institutionId, @Param("childId") Long childId);
}
